using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BusinessLayer;

public partial class inventory_ManageInventory : System.Web.UI.Page
{
    BL_UPC upcBL = new BL_UPC();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlItemType.DataSource = upcBL.PopulateItemGroup();
            ddlItemType.DataTextField = "type";
            ddlItemType.DataValueField = "type_idx";
            ddlItemType.DataBind();
        }
    }
    protected void ddlItemType_SelectedIndexChanged(object sender, EventArgs e)
    {
        dgInventory.DataSource = upcBL.PopulateInventory(Convert.ToInt32(ddlItemType.SelectedValue));
        dgInventory.DataMember = "Inventory";
        dgInventory.DataBind();
    }

    protected void dgInventory_EditCommand(object source, DataGridCommandEventArgs e)
    {
        dgInventory.EditItemIndex = e.Item.ItemIndex;

        dgInventory.DataSource = upcBL.PopulateInventory(Convert.ToInt32(ddlItemType.SelectedValue));
        dgInventory.DataMember = "inventory";
        dgInventory.DataBind();
    }

    //retrieve Items for databinding.
    protected string RetrieveSpecificType(string itemID)
    {
        BL_UPC.TypeStructure Type = new BL_UPC.TypeStructure();
        Type = upcBL.RetrieveSpecificType(Convert.ToInt32(itemID));
        return Type.type;
    }

    protected bool TorF(string testval)
    {
        if (testval == "1")
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    protected DataSet PopulateItemTypes()
    {
        return upcBL.PopulateItemGroup();
    }

    protected void dgInventory_CancelCommand(object source, DataGridCommandEventArgs e)
    {
        dgInventory.EditItemIndex = -1;

        dgInventory.DataSource = upcBL.PopulateInventory(Convert.ToInt32(ddlItemType.SelectedValue));
        dgInventory.DataMember = "inventory";
        dgInventory.DataBind();
    }

    protected string populateTags(string upc)
    {
        return upcBL.RetrieveTagList(upc);
    }
    protected void dgInventory_UpdateCommand(object source, DataGridCommandEventArgs e)
    {
        BL_UPC.InventoryStructure Inv = new BL_UPC.InventoryStructure();
        TextBox tb;
        DropDownList ddl;
        Label lbl;

        lbl = (Label)e.Item.Cells[1].FindControl("lblUPC");
        Inv.upc = lbl.Text;

        tb = (TextBox)e.Item.Cells[2].FindControl("tbInStock");
        Inv.instock = Convert.ToInt32(tb.Text);

        ddl = (DropDownList)e.Item.Cells[3].FindControl("ddlSpecification");
        Inv.itemType = Convert.ToInt32(ddl.SelectedValue);

        tb = (TextBox)e.Item.Cells[4].FindControl("tbPrice");
        Inv.lastPrice = Convert.ToDouble(tb.Text);

        upcBL.UpdateUPC(Inv);

        dgInventory.EditItemIndex = -1;

        dgInventory.DataSource = upcBL.PopulateInventory(Convert.ToInt32(ddlItemType.SelectedValue));
        dgInventory.DataMember = "inventory";
        dgInventory.DataBind();
    }
}
    